/* win_app.c is part of VIP program
 * Written by J. Salvador Arias, 2010
 *      CONICET, INSUE, Instituto Miguel Lillo,
 *      Miguel Lillo 205, S.M. de Tucuman (4000)
 *      Tucuman, Argentina
 *      e-mail: jsalarias@csnat.unt.edu.ar
 *
 * VIP is a free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation and
 * Creative Commons.
 *
 * This file and the VIP program is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * To view a copy of this license, visit
 * http://creativecommons.org/licenses/GPL/2.0/
 * or send a letter to
 * Creative Commons, 171 Second Street, Suite 300,
 * San Francisco, California, 94105, USA.
 */

#include "wins.h"
#include "biogeo.h"
#include "parser.h"
#include "progbar.h"

void OnError () {
 GtkWidget* dialog;

 dialog = gtk_message_dialog_new (GTK_WINDOW (GETMAINWIN), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
                                  errorMessage);
 gtk_dialog_run (GTK_DIALOG (dialog));
 gtk_widget_destroy (dialog);
 RESETERROR;
}

void UpdateOnCalculus () {
 while (gtk_events_pending ())
   gtk_main_iteration ();
}

int main (int argc, char** argv) {
 GTimeVal actTime;

 gtk_init (&argc, &argv);
 g_get_current_time (&actTime);
 g_random_set_seed (actTime . tv_usec);
 GETMAINWIN = NULL;
 outLog = g_string_sized_new (1024);
 StartBioGeo ();
 StartTreeViewWindow ();
 StartMapWindow ();
 StartMainWindow ();

 gtk_main ();
 return 0;
}

GtkWidget* CreateWindow (gchar* name, gint x, gint y, gint sizeX, gint sizeY, GCallback OnDelete, GtkWidget* box) {
 GtkWidget* window;

 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 gtk_window_set_title (GTK_WINDOW (window), name);
 gtk_window_move (GTK_WINDOW (window), x, y);
 gtk_window_set_default_size (GTK_WINDOW (window), sizeX, sizeY);
 gtk_container_border_width (GTK_CONTAINER (window), 0);
 g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (OnDelete), NULL);
 if (box != NULL) {
   gtk_container_add (GTK_CONTAINER (window), box);
   gtk_widget_show (box);
 }
 return window;
}

GtkWidget* AddMenuToBar (GtkWidget* menu, gchar* name, GtkWidget* menuBar) {
 GtkWidget* menuItem;

 menuItem = gtk_menu_item_new_with_mnemonic (name);
 gtk_widget_show (menuItem);
 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuItem), menu);
 gtk_menu_bar_append (GTK_MENU_BAR (menuBar), menuItem);
 return menuItem;
}

void CreateMenuSeparator (GtkWidget* menu) {
 GtkWidget* menuItem;

 menuItem = gtk_menu_item_new ();
 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItem);
 gtk_widget_show (menuItem);
}

GtkWidget* CreateMenuItem (GtkWidget* menu, gchar* name, GtkAccelGroup* accelGroup, guint key,
                           GdkModifierType modifier, GCallback func, gpointer data) {
 GtkWidget* menuItem;

 menuItem = gtk_menu_item_new_with_mnemonic (name);
 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItem);
 g_signal_connect (G_OBJECT (menuItem), "activate", G_CALLBACK (func), data);
 gtk_widget_show (menuItem);
 if (accelGroup != NULL)
   gtk_widget_add_accelerator (menuItem, "activate", accelGroup, key, modifier, GTK_ACCEL_VISIBLE);
 return menuItem;
}

GtkWidget* CreateMenuItemCheck (GtkWidget* menu, gchar* name, GtkAccelGroup* accelGroup, guint key,
                                GdkModifierType modifier, GCallback func, gpointer data) {
 GtkWidget* menuItem;

 menuItem = gtk_check_menu_item_new_with_mnemonic (name);
 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItem);
 g_signal_connect (G_OBJECT (menuItem), "activate", G_CALLBACK (func), data);
 gtk_widget_show (menuItem);
 if (accelGroup != NULL)
   gtk_widget_add_accelerator (menuItem, "activate", accelGroup, key, modifier, GTK_ACCEL_VISIBLE);
 return menuItem;
}

GtkWidget* CreateMenuItemRadio (GtkWidget* menu, gchar* name, GtkAccelGroup* accelGroup, guint key,
                                GdkModifierType modifier, GCallback func, gpointer data, GSList* group) {
 GtkWidget* menuItem;

 menuItem = gtk_radio_menu_item_new_with_mnemonic (group, name);
 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItem);
 g_signal_connect (G_OBJECT (menuItem), "activate", G_CALLBACK (func), data);
 gtk_widget_show (menuItem);
 if (accelGroup != NULL)
   gtk_widget_add_accelerator (menuItem, "activate", accelGroup, key, modifier, GTK_ACCEL_VISIBLE);
 return menuItem;
}

GtkFileFilter* CreateFileFilter (gchar* name, gchar* pattern, GtkWidget* fileChooser) {
 GtkFileFilter* filter;

 filter = gtk_file_filter_new ();
 gtk_file_filter_set_name (filter, name);
 gtk_file_filter_add_pattern (filter, pattern);
 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (fileChooser), filter);
 return filter;
}

GtkWidget* CreateEntry (gchar* label, GtkBox* box) {
 GtkWidget* entryLabel;
 GtkWidget* toRet;

 entryLabel = gtk_label_new (label);
 gtk_box_pack_start (GTK_BOX (box), entryLabel, TRUE, TRUE, 0);
 gtk_widget_show (entryLabel);
 toRet = gtk_entry_new ();
 gtk_box_pack_start (GTK_BOX (box), toRet, TRUE, TRUE, 0);
 gtk_widget_show (toRet);
 return toRet;
}
